package com.example.ui.learn;

import java.util.HashMap;

import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.tencent.qq.QQ;
import cn.sharesdk.tencent.qzone.QZone;
import cn.sharesdk.tencent.weibo.TencentWeibo;
import cn.sharesdk.wechat.friends.Wechat;
import cn.sharesdk.wechat.moments.WechatMoments;

import com.example.huashangapp.R;
import com.example.util.Util;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;

import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.TextView;

public class ShareActivity extends Activity {
	TextView shareQQ,QQZone,shareWechat,shareMoment,shareQQweibo,shareMessage;
	Platform.ShareParams sp;
	private String filepath;
	private String imageUrl;
	private String title;
	private String name;
	// 滑上来的动画
	private Animation animShow;
	// 滑下去的动画
	private String musicUrl;
	private Animation animHide;
	private FrameLayout flPage;
	private Button shareCancel;
	@Override
	protected void onCreate(Bundle savedInstanceState) {		
		super.onCreate(savedInstanceState);
		imageUrl=getIntent().getStringExtra("imageUrl");
		title=getIntent().getStringExtra("title");
		name=getIntent().getStringExtra("name");
		musicUrl=getIntent().getStringExtra("musicUrl");
		initAnim();
		// 显示列表
		flPage= (FrameLayout) this.getLayoutInflater().inflate(R.layout.a_share,null);
		flPage.clearAnimation();
		flPage.startAnimation(animShow);
		setContentView(flPage);
		ShareSDK.initSDK(ShareActivity.this);
		
		shareQQ=(TextView) findViewById(R.id.shareQQ);
		QQZone=(TextView)findViewById(R.id.QQZone);
		shareWechat=(TextView) findViewById(R.id.shareWechat);
		shareMoment=(TextView) findViewById(R.id.shareMoment);
		//shareSinaweibo=(TextView) findViewById(R.id.shareSinaweibo);
		shareQQweibo=(TextView) findViewById(R.id.shareQQweibo);
		shareCancel=(Button)findViewById(R.id.shareCancel);
		shareMoment.setOnClickListener(sa_ocl);
		shareQQ.setOnClickListener(sa_ocl);
		QQZone.setOnClickListener(sa_ocl);
		shareQQweibo.setOnClickListener(sa_ocl);
		//shareSinaweibo.setOnClickListener(sa_ocl);
		shareWechat.setOnClickListener(sa_ocl);
		shareCancel.setOnClickListener(sa_ocl);
	}
	private void initAnim() {
		animShow = new TranslateAnimation(
				Animation.RELATIVE_TO_SELF, 0,
				Animation.RELATIVE_TO_SELF, 0,
				Animation.RELATIVE_TO_SELF, 1,
				Animation.RELATIVE_TO_SELF, 0);
		animShow.setDuration(300);

		animHide = new TranslateAnimation(
				Animation.RELATIVE_TO_SELF, 0,
				Animation.RELATIVE_TO_SELF, 0,
				Animation.RELATIVE_TO_SELF, 0,
				Animation.RELATIVE_TO_SELF, 1);
		animHide.setDuration(300);
	}
	PlatformActionListener paListener=new PlatformActionListener() {
		
		@Override
		public void onError(Platform arg0, int arg1, Throwable arg2) {
			handler.sendEmptyMessage(1);
		}
		
		@Override
		public void onComplete(Platform arg0, int arg1, HashMap<String, Object> arg2) {
			handler.sendEmptyMessage(2);
		}
		
		@Override
		public void onCancel(Platform arg0, int arg1) {
			handler.sendEmptyMessage(3);
		}
	};
	
	OnClickListener sa_ocl=new OnClickListener() {

		

		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.shareQQ:
				Platform qq=ShareSDK.getPlatform(ShareActivity.this,QQ.NAME);
				QQ.ShareParams QQ_sp=new QQ.ShareParams();
				QQ_sp.title = getString(R.string.share_title);
				QQ_sp.text ="【"+name+"】系列课程之【"+title+"】";
				QQ_sp.imageUrl=imageUrl;
				QQ_sp.musicUrl=musicUrl;
				QQ_sp.titleUrl="http://fl.act.qq.com/17373/addev/h5/39220";
				qq.getPlatformActionListener();
				qq.share(QQ_sp);
				break;
			case R.id.QQZone:
				Platform QQZone=ShareSDK.getPlatform(ShareActivity.this,QZone.NAME);
				QZone.ShareParams QQZone_sp=new QZone.ShareParams();
				// 执行图文分享
				QQZone_sp.title="华商学习网分享";
				QQZone_sp.text ="【"+name+"】系列课程之【"+title+"】";
				QQZone_sp.titleUrl="http://fl.act.qq.com/17373/addev/h5/39220";
				QQZone_sp.imageUrl=imageUrl;
				QQZone_sp.site=getString(R.string.share_title);
				QQZone_sp.siteUrl="http://fl.act.qq.com/17373/addev/h5/39220";
				QQZone.getPlatformActionListener();
				QQZone.share(QQZone_sp);
				break;
			case R.id.shareMoment:
				Platform moments=ShareSDK.getPlatform(ShareActivity.this,WechatMoments.NAME);
				WechatMoments.ShareParams moments_sp=new WechatMoments.ShareParams();
				moments_sp.shareType = Platform.SHARE_MUSIC;
				moments_sp.title = getString(R.string.share_title);
				moments_sp.text ="【"+name+"】系列课程之【"+title+"】";
				moments_sp.imageUrl=imageUrl;
				moments_sp.musicUrl=musicUrl;
				moments_sp.url = "http://fl.act.qq.com/17373/addev/h5/39220";
				moments.getPlatformActionListener();
				moments.share(moments_sp);
				break;
			case R.id.shareWechat:
				Platform wechat=ShareSDK.getPlatform(ShareActivity.this,Wechat.NAME);
				Wechat.ShareParams Wechat_sp=new Wechat.ShareParams();
				Wechat_sp.title = getString(R.string.share_title);
				Wechat_sp.text ="【"+name+"】系列课程之【"+title+"】";
				Wechat_sp.imageUrl=imageUrl;
				Wechat_sp.shareType = Platform.SHARE_MUSIC;
				Wechat_sp.musicUrl=musicUrl;
				Wechat_sp.url = "http://fl.act.qq.com/17373/addev/h5/39220";
				wechat.getPlatformActionListener();
				wechat.share(Wechat_sp);
				break;
			case R.id.shareQQweibo:
				Platform QQweibo=ShareSDK.getPlatform(ShareActivity.this,TencentWeibo.NAME);
				QQweibo.getPlatformActionListener();
				TencentWeibo.ShareParams QQweibo_sp=new TencentWeibo.ShareParams();
				QQweibo_sp.text = "【"+name+"】系列课程之"+title+"点击下载http://fl.act.qq.com/17373/addev/h5/39220";
				QQweibo_sp.imageUrl=imageUrl;
				QQweibo.share(QQweibo_sp);
				break;
			case R.id.shareCancel:
				handler.sendEmptyMessage(3);
				finish();
				break;
			default:
				break;
			}
			finish();
		}
	};
	Handler handler=new Handler(){

		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 1:
				Util.toast(ShareActivity.this, "分享出错！");
				break;
			case 2:
				Util.toast(ShareActivity.this, "分享成功！");
				break;
			case 3:
				Util.toast(ShareActivity.this, "取消分享！");
				break;
			default:
				break;
			}
			super.handleMessage(msg);
		}
		
	};
	@Override
	protected void onDestroy() {
		
		ShareSDK.stopSDK(this);
		super.onDestroy();
	}

}
